package com.sunny.l77;

public class Solution {
    public int minDistance(String word1, String word2) {
        if(null == word1 && null == word2){
            return 0;
        }else if(null == word1){
            return word2.length();
        }else if(null == word2){
            return word1.length();
        }
        int len1 = word1.length();
        int len2 = word2.length();
        if(len1 == 0){
            return len2;
        }
        if(len2 == 0){
            return len1;
        }
        int[][] dp = new int[len1+1][len2+1];
        dp[0][0] = 0;
        for(int i=1;i<=len1;i++){
            dp[i][0] = dp[i-1][0]+1;
        }
        for(int j=1;j<=len2;j++){
            dp[0][j] = dp[0][j-1]+1;
        }
        for(int i=0;i<len1;i++){
            for(int j=0;j<len2;j++){
                if(word1.charAt(i) == word2.charAt(j)){
                    dp[i+1][j+1] = dp[i][j];
                }else{
                    dp[i+1][j+1] = Integer.min((Integer.min(dp[i][j+1], dp[i+1][j])), dp[i][j])+1;
                }
            }
        }
        return dp[len1][len2];
    }
}